# -*- coding: utf_8 -*-
# @Create   : 2021/9/18 13:50
# @Author   : yh
# @Remark   : 定时任务db层
import logging

from mxsoftpy import DB
from mxsoftpy.exception import DBError, DataError


class TimedTaskDB(DB):

    def create(self, **kwargs) -> str:
        """
        新增定时任务
        """
        task_code = self.tree.open('Co', 'Co_1\\Config\\TimedTask', file='master', path_flag=True
                                   ).insert_key_items(kwargs)
        logging.info('新增了一个定时任务到数据库，任务code：%s' % task_code)
        self.mq.open('JianKongYi-TimedTask').push('Co_1;%s' % task_code, label='TIMEDTASK:ADDNEW')
        logging.info('发送新增定时任务消息到mq成功，推送给c的服务')
        return task_code

    def delete(self, timed_task_code: str, **kwargs):
        """
        删除定时任务
        :param timed_task_code: 要删除的任务code
        """
        try:
            self.tree.open('Co', 'Co_1\\Config\\TimedTask\\%s' % timed_task_code, file='master')
        except DBError as e:
            logging.error('删除定时任务时：打开定时任务失败，定时任务code：%s，错误信息：%s' % (timed_task_code, str(e)))
            raise DataError('删除定时任务失败')
        self.tree.insert_items(kwargs)
        logging.info('逻辑删除定时任务成功，任务code：%s' % timed_task_code)
        self.mq.open('JianKongYi-TimedTask').push('Co_1;%s' % timed_task_code, label='TIMEDTASK:DELETE')
        logging.info('发送删除定时任务消息到mq成功，推送给C++的服务')
        return timed_task_code
